<?php

session_start();
require_once('config.inc.php');
require_once('DatabaseManager.php');

$db = new DatabaseManager;
$db -> createDB();

PRINT "<html>";
PRINT "<head><title>Meta-Daten eingeben</title></head>";
PRINT "<body>";

$dbname = DB_NAME;

$current_user = $_SESSION['user_id'];

if(!@$title = $_POST["title"])
		$title = '';
if(!@$author = $_POST["author"])
		$author = '';
if(!@$description = $_POST["description"])
		$description = '';
if(!@$tag1 = $_POST["tag1"])
		$tag1 = '';
if(!@$tag2 = $_POST["tag2"])
		$tag2 = '';
if(!@$tag3 = $_POST["tag3"])
		$tag3 = '';
if(!@$tag4 = $_POST["tag4"])
		$tag4 = '';
if(!@$tag5 = $_POST["tag5"])
		$tag5 = '';

/* make connection to database */
MYSQL_CONNECT(DB_SERVER,DB_USER_NAME,DB_USER_PASSWORD) OR DIE("Could not connect to Database!");
@mysql_select_db("$dbname") or die("Database not found!"); 

PRINT "Folgende Daten wurden eingegeben:<br>";
PRINT "<ul>";
PRINT "<li>Titel: <em>$title</em>";
PRINT "<li>Autor: <em>$author</em>";
PRINT "<li>Beschreibung: <em>$description</em>";
PRINT "<li>Tag1: <em>$tag1</em>";
PRINT "<li>Tag2: <em>$tag2</em>";
PRINT "<li>Tag3: <em>$tag3</em>";
PRINT "<li>Tag4: <em>$tag4</em>";
PRINT "<li>Tag5: <em>$tag5</em>";
PRINT "</ul>";
PRINT "<hr>";

class InsertMetaData
{
	function insertData($title,$current_user, $author, $description, $tag1, $tag2, $tag3, $tag4, $tag5)
	{
		/* MySQL table created to store the data */
		$tablename = "metadata";
		

		$query1 = "SELECT * FROM $tablename WHERE title = '$title' AND author = '$author'";
		$result1 = MYSQL_QUERY($query1);
	  $num_rows =  mysql_num_rows($result1);
		
		if(!$num_rows)
		{
			/* Insert information into table */
			$query2 = "INSERT INTO $tablename VALUES('NULL', $current_user,'$title', '$author', CURRENT_DATE(), CURRENT_DATE(), '$description', '$tag1', '$tag2', '$tag3', '$tag4', '$tag5')";
			$result2 = MYSQL_QUERY($query2);
			
			PRINT "Antwort des Datenbank-Servers:<br>"; 
			if($result2)
			{
			   $affected_rows =  mysql_affected_rows();
			   PRINT "<strong>Query OK. Betroffene Zeilen: $affected_rows</strong>";
			}
			else
			{
			   PRINT "<strong>Query FAILED. Sie haben m&ouml;glicherweise nicht die ben&ouml;tigten Rechte!</strong>";
			}
		}
		else
		{
			PRINT "Metadaten sind bereits in der Datenbank!<br>";
			PRINT "<a href=\"metadata.html\">Erneut eingeben</a>";			
		}
	}
	
	function editData($meta_id, $title, $author, $description, $tag1, $tag2, $tag3, $tag4, $tag5)
	{
		$query2 = "SELECT * FROM metadata WHERE meta_id = '$meta_id'";
		$result2 = mysql_query($query2) or die(mysql_error()); 

		while($row2 = mysql_fetch_object($result2)) 
		{ 
			$creation_date = $row2->creation_date;
		} 
		
    $query3  = "UPDATE metadata SET title = '$title', author = '$author', creation_date = '$creation_date',";
    $query3 .= "last_edit = CURRENT_DATE(), description = '$description', tag1 = '$tag1',";
    $query3 .= "tag2 = '$tag2', tag3 = '$tag3', tag4 = '$tag4', tag5 = '$tag5' WHERE meta_id  = '$meta_id'";
		$result3 = MYSQL_QUERY($query3);
	}
}

$metadata = new InsertMetaData();
/* if meta_id is sent, editData() */
$meta_id	= $_GET['meta_id']; 

if($meta_id)
{
	$metadata->editData($meta_id, $title, $author, $description, $tag1, $tag2, $tag3, $tag4, $tag5);
}
else
{
	$metadata->insertData($title, $current_user,$author, $description, $tag1, $tag2, $tag3, $tag4, $tag5);	
}

$query  = "SELECT * FROM metadata WHERE title = '$title' AND author = '$author' AND description = '$description'";
$query .= "AND tag1 = '$tag1' AND tag2 = '$tag2' AND tag3 = '$tag3' AND tag4 = '$tag4' AND tag5 = '$tag5'";
$result = mysql_query($query) or die(mysql_error()); 

while($row = mysql_fetch_object($result)) 
{ 
	$meta_id = $row->meta_id;
}    

PRINT "<br>";
PRINT "<a href=\"index.php?action=category_choosen\">OK</a>";
PRINT "<a href=\"metadata_edit.php?meta_id=$meta_id\">Bearbeiten</a>";
PRINT "</body>";
PRINT "</html>";

/* Close the database connection */
MYSQL_CLOSE();
?>